package com.manor.common.dialog

import android.app.Activity
import android.os.CountDownTimer
import android.util.Log
import android.view.View
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.interfaces.SimpleCallback
import com.manor.common.CommonAppConfig
import com.manor.common.R
import com.manor.common.http.JCommonHttpUtil
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.ClickUtil
import com.manor.common.utils.ToastUtil
import com.manor.common.utils.str
import com.manor.common.utils.strIsNull
import kotlinx.android.synthetic.main.dialog_x_code_img.view.*
import java.util.*


/**
 *
 */
class XCodeImgPopup(val mAct: Activity,
                    val vCode: String,
                    val timeStamp: String,
                    val taskType: String,
                    val timeCount: Int,
                    val listener: (Boolean) -> Unit)
    : CenterPopupView(mAct), View.OnClickListener {
    private val imgHost = CommonAppConfig.JAVA_HOST + "admin-sms/app/sms/getCaptcha?type=1&randomCode="
    private var random = ""


    companion object {
        private var pressedTime: Long = 0
        fun load(context: Activity, vCode: String, timeStamp: String, taskType: String, timeCount: Int,
                 listener: (Boolean) -> Unit) {
            XPopup.Builder(context)
                    .dismissOnTouchOutside(false)
                    .isDestroyOnDismiss(true)
                    .hasBlurBg(true)
                    .setPopupCallback(object : SimpleCallback() {
                        override fun onBackPressed(popupView: BasePopupView?): Boolean {
                            return if (System.currentTimeMillis() - pressedTime < 2000) {
                                super.onBackPressed(popupView)
                            } else {
                                ToastUtils.showShort("再按一次退出,但做任务将失败")
                                pressedTime = System.currentTimeMillis()
                                true
                            }
                        }
                    })
                    .asCustom(XCodeImgPopup(context, vCode, timeStamp, taskType, timeCount, listener))
                    .show()
        }
    }

    override fun getImplLayoutId(): Int = R.layout.dialog_x_code_img


    override fun onCreate() {
        super.onCreate()
        tvTime?.text = "剩余${timeCount}s"
        ivImg.setOnClickListener(this)
        tvConfirm.setOnClickListener(this)
        getCodeImg()

        var countDownTimer = object : CountDownTimer(timeCount * 1000L, 1000) {
            override fun onFinish() {
                dismiss()
            }

            override fun onTick(ms: Long) {
                tvTime?.text = "剩余${ms / 1000}s"
            }
        }.start()

    }

    override fun dismiss() {
        super.dismiss()
    }

    private fun doTask() {
        JCommonHttpUtil.getAdTask("", vCode, timeStamp, random, etCode.str(), taskType, object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String?, info: String) {
                if (code == 1511) {
                    getCodeImg()
                }
                msg?.let { ToastUtil.show(it) }
                if (code != 200) {
                    return
                }
                listener.invoke(true)
                dismiss()

            }
        })
    }


    private fun getCodeImg() {
        random = UUID.randomUUID().toString()
        Glide.with(context)
                .load(imgHost.plus(random))
                .apply {
                    this.transform(CenterCrop(), RoundedCorners(ConvertUtils.dp2px(18f)))
                }
                .into(ivImg)

    }

    override fun onClick(v: View?) {
        if (ClickUtil.canClick(500))
            when (v?.id) {
                R.id.ivImg -> {
                    getCodeImg()
                }
                R.id.tvConfirm -> {
                    if (etCode.strIsNull()) {
                        ToastUtils.showShort("请输入图形验证码")
                        return
                    }
                    doTask()
                }
            }
    }


}